package com.prs.crm.action.approval;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.approval.TableCss;
import com.prs.crm.domain.approval.TestJdbc;
import com.prs.crm.service.approval.TestJdbcService;

public class TestJdbcAction extends BaseAction{
	private String DBName;
	private List<String> drivers;
	private String conn;
	private String driver;
	private String username;
	private String userpwd;
	private PaginationSupport pagination;
	private Integer[] ids;
	private TestJdbcService testJdbcService;
	private TestJdbc testJdbc;
	public String getDBName() {
		return DBName;
	}
	public void setDBName(String dBName) {
		DBName = dBName;
	}
	public List<String> getDrivers() {
		return drivers;
	}
	public void setDrivers(List<String> drivers) {
		this.drivers = drivers;
		
	}	
	public String getConn() {
		return conn;
	}
	public void setConn(String conn) {
		this.conn = conn;
	}
	public String getDriver() {
		return driver;
	}
	public void setDriver(String driver) {
		this.driver = driver;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUserpwd() {
		return userpwd;
	}
	public void setUserpwd(String userpwd) {
		this.userpwd = userpwd;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public TestJdbcService getTestJdbcService() {
		return testJdbcService;
	}
	public void setTestJdbcService(TestJdbcService testJdbcService) {
		this.testJdbcService = testJdbcService;
	}
	public TestJdbc getTestJdbc() {
		return testJdbc;
	}
	public void setTestJdbc(TestJdbc testJdbc) {
		this.testJdbc = testJdbc;
	}
	//////////////////////////////////////////////////////////////////////
	@Action("listTestJdbc")
	public String listTestJdbc(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(TestJdbc.class);
		this.setPagination(this.testJdbcService.listByPage(criteria, 
				this.getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	@Action("editTestJdbc")
	public String editTestJdbc(){
		return SUCCESS;
	}
	@Action(value="saveTestJdbc",results={
			@Result(name="success",type="redirect",location="listTestJdbc"),
			@Result(name="error",type="chain",location="editTestJdbc")
	})
	public String saveTestJdbc(){
		this.testJdbcService.save(testJdbc);
		return SUCCESS;
	}
	@Action(value="removeTestJdbc",results={
			@Result(name="success",type="redirect",location="listTestJdbc"),
			@Result(name="error",type="chain",location="listTestJdbc")
	})
	public String removeTestJdbc(){
		if(ids==null){
			this.setPrompt("请选择数据行！");
			return ERROR;
		}
		this.testJdbcService.batchRemove(ids);
		return SUCCESS;
	}
	
	//测试连接
	@Action(value="testjdbc",results={
			@Result(name="success",type="json",params={"String","manage"})
	})
	public String test(){
		Connection conn = null;
		String manage = "";
		try {
			Class.forName(this.driver);
			conn = DriverManager.getConnection(this.conn, this.username, this.userpwd);
			manage = "连接成功！";
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			manage = "连接失败！";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			manage = "连接失败！";
		}
		
		return SUCCESS;
	}
	
}
